package org.lab.tracecash.services;

import javax.inject.Inject;
import javax.inject.Provider;
import javax.persistence.EntityManager;

import org.lab.tracecash.Constants;
import org.lab.tracecash.model.Account;
import org.lab.tracecash.model.User;

public class CommonAccountGenerator {

	@Inject
	private Provider<EntityManager> entityManagerProvider;

	public void generateCommonsAccounts(User user) {
		createAndPersist(Constants.ACC_VOID, user, null);
		createAndPersist(Constants.ACC_UNCLASSIFIED, user, null);
		createAndPersist(Constants.ACC_CASH_IN_WALLET, user, null);
		createAndPersist(Constants.ACC_ADJUSTEMENT, user, null);

		Account income = createAndPersist(Constants.ACC_INCOME, user, null);
		createAndPersist(Constants.ACC_SALARY, user, income);
		createAndPersist(Constants.ACC_GIFTS_RECEIVED, user, income);

		Account expenses = createAndPersist(Constants.ACC_EXPENSES, user, null);
		createAndPersist(Constants.ACC_HOME, user, expenses);
		createAndPersist(Constants.ACC_PHONE, user, expenses);
		createAndPersist(Constants.ACC_TAXES, user, expenses);
		createAndPersist(Constants.ACC_HOBBIES, user, expenses);
		createAndPersist(Constants.ACC_GIFTS, user, expenses);
		createAndPersist(Constants.ACC_CLOTHES, user, expenses);
		createAndPersist(Constants.ACC_COMPUTER, user, expenses);
		createAndPersist(Constants.ACC_EDUCATION, user, expenses);
		createAndPersist(Constants.ACC_RENT, user, expenses);
		createAndPersist(Constants.ACC_FOOD, user, expenses);
		createAndPersist(Constants.ACC_FOOD_HOME, user, expenses);
		createAndPersist(Constants.ACC_BANK_FEES, user, expenses);
		createAndPersist(Constants.ACC_PETS, user, expenses);
		createAndPersist(Constants.ACC_PUBLIC_TRANSPORTATION, user, expenses);
		createAndPersist(Constants.ACC_SUBSCRIPTIONS, user, expenses);
		createAndPersist(Constants.ACC_HEALTH, user, expenses);
		createAndPersist(Constants.ACC_GAMES, user, expenses);
		createAndPersist(Constants.ACC_ENTERTAINMENT, user, expenses);
		createAndPersist(Constants.ACC_LEISURES, user, expenses);

		createAndPersist(Constants.ACC_AUTO, user, expenses);
		createAndPersist(Constants.ACC_AUTO, user, expenses);
	}

	private Account createAndPersist(String name, User owner, Account parent) {
		Account account = new Account(name, owner);
		entityManagerProvider.get().persist(account);
		return account;
	}
}
